#include "demo_v1_User.h"
#include "Common.h"
#include "hiredis/hiredis.h"
#include "mysql.h"
#include "paho-mqtt/MQTTClient.h"
#include "utils/RedisSingleton.h"
#include "utils/MysqlSingleton.h"
#include "utils/MqttSingleton.h"
#include "time.h"

#define ADDRESS     "tcp://test.bodyta.com:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "cmqtt"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

using namespace demo::v1;
using namespace std;
//add definition of your processing function here
extern vector<WebSocketConnectionPtr> *wsList;
void redis();
void mysql();
void mqtt();

void User::login(const HttpRequestPtr &req,
                 std::function<void(const HttpResponsePtr &)> &&callback,
                 std::string &&userId,
                 const std::string &password) {
    LOG_DEBUG << "User " << userId << " login";
    //认证算法，读数据库，验证身份等...
    //...
    Json::Value ret;
    ret["result"] = "ok";
    ret["token"] = drogon::utils::getUuid();

    auto resp = HttpResponse::newHttpJsonResponse(ret);
    callback(resp);
}

void User::getInfo(const HttpRequestPtr &req,
                   std::function<void(const HttpResponsePtr &)> &&callback,
                   std::string userId,
                   const std::string &token) const {
    LOG_DEBUG << "User " << userId << " get his information";
    //验证token有效性等
    //读数据库或缓存获取用户信息
    Json::Value ret;
    ret["result"] = "ok";
    ret["user_name"] = "Jack";
    ret["user_id"] = userId;
    ret["gender"] = 1;
    auto resp = HttpResponse::newHttpJsonResponse(ret);
    callback(resp);
}

void User::sendWs(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
//    std::cout << NsCommon::wsList->size() << std::endl;
//    for (int i = 0; i < NsCommon::wsList->size(); ++i) {
//        NsCommon::wsList->at(i)->send("111");
//        LOG_DEBUG << "SEND IFNO";
//    }
//    cout << "aaaa  " << NsCommon::a << endl;
    Json::Value ret;

    ret["timestamp"]=time(0);
    ret["result"] = "ok";
//    string result=RedisSingleton::GetInstance().DoSomething();
//    string mysqlRes=MysqlSingleton::GetInstance().doSomething();

    MqttSingleton::getMqttSingleton().doSomething();

   // redis();
    //mysql();
    //mqtt();
    auto resp = HttpResponse::newHttpJsonResponse(ret);
    callback(resp);
}


